package com.qinsong.myapplication.controller.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;
import com.qinsong.myapplication.R;
import com.qinsong.myapplication.model.Model;
import com.qinsong.myapplication.model.bean.UserInfo;
import com.qinsong.myapplication.utils.SaveLoginAndRegisterInfo;
import com.qinsong.myapplication.utils.ScreenUtils;
;import java.util.Map;

public class LoginActivity extends Activity {
    private EditText username;
    private EditText password;
    private Button register;
    private Button login;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ScreenUtils.setFullScreen(this, true);
        //初始化控件
        InitView();
        //初始化监听
        InitListener();
    }

    /**
     * 初始化空间
     */
    private void InitView() {
        username= findViewById(R.id.username);
        password=findViewById(R.id.password);
        register= findViewById(R.id.register);
        login=findViewById(R.id.login);
        Map<String,String> userMap = SaveLoginAndRegisterInfo.getUserInfo(this);
        if (userMap!=null){
            username.setText(userMap.get("username"));
            password.setText(userMap.get("password"));
        }
    }

    /**
     * 点击事件监听
     */
    private void InitListener() {
        //注册按钮的点击事件处理
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    toRegister();
            }
        });
        //登录按钮点击事件
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Login();
            }
        });
    }
    //登录验证
    private void Login() {
        final String loginname = username.getText().toString();
        final String loginpassword = password.getText().toString();
        //校验输入的用用户名密码
        if (TextUtils.isEmpty(loginname) || TextUtils.isEmpty(loginpassword)){
            Toast.makeText(LoginActivity.this,"用户名密码不能为空",Toast.LENGTH_SHORT).show();
            return;
        }

        //登录逻辑
        Model.getInstance().getGlobaThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                EMClient.getInstance().login(loginname, loginpassword, new EMCallBack() {
                    //成功后
                    @Override
                    public void onSuccess() {
                        //对模型层数据处理
                        Model.getInstance().loginSuccess(new UserInfo(loginname));
                        //保存用户账户保存到本地数据库
                        Model.getInstance().getUserAccountDao().addUser(new UserInfo(loginname));
                        SaveLoginAndRegisterInfo.saveUserInfo(LoginActivity.this,loginname,loginpassword);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                //提示登录成功
                                Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
                                //记住账号密码

                                //跳转页面
                                Intent intent = new Intent(LoginActivity.this,indexActivity.class);
                                startActivity(intent);
                                finish();
                            }
                        });

                    }
                    //失败
                    @Override
                    public void onError(int i, final String s) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    //提示登录失败
                                    Toast.makeText(LoginActivity.this,"登录失败"+s,Toast.LENGTH_SHORT).show();

                                }
                            });
                    }
                    //过程中
                    @Override
                    public void onProgress(int i, String s) {

                    }
                });
            }
        });

    }

    /**
     * 跳转注册页面
     */
    private void toRegister(){
        Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
        startActivity(intent);
        finish();
    }

    //注册验证
   /* private void Register() {
        //获取输入的用户名和密码
        final String reusername = username.getText().toString();
        final String repassword = password.getText().toString();
        //校验输入的用用户名密码
        if (TextUtils.isEmpty(reusername) || TextUtils.isEmpty(repassword)){
            Toast.makeText(LoginActivity.this,"用户名密码不能为空",Toast.LENGTH_SHORT).show();
            return;
            }
        //去环信的服务器注册账号
        Model.getInstance().getGlobaThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                try {
                    //注册信息
                    EMClient.getInstance().createAccount(reusername,repassword);
                    //更新页面
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(LoginActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
                        }
                    });
                } catch (final HyphenateException e) {
                    e.printStackTrace();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(LoginActivity.this,"注册失败"+e.toString(),Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        });
    }*/


}
